package com.gxt.riaapp.client.mvc;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.CheckMenuItem;
import com.extjs.gxt.ui.client.widget.menu.DateMenu;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.menu.SeparatorMenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.gxt.riaapp.client.RiaApp;
import com.gxt.riaapp.client.mvc.model.Entry;
import com.gxt.riaapp.client.mvc.model.Page;

public class AppView extends View {
	private SelectionListener<MenuEvent> buttonListener= new SelectionListener<MenuEvent>() {
		@Override
		public void componentSelected(MenuEvent ce) {
			// TODO Auto-generated method stub
			RiaApp.showPage( new Entry("요건","0012", new CopyOfSmplProgram("하하하")));
		}  
	};
	public static final String CENTER_PANEL = "centerPanel";
	public static final String WEST_PANEL = "westPanel";
	public static final String NORTH_PANEL = "northPanel";
	public static final String VIEWPORT = "viewPort";
	
	private Viewport 		viewport; 	// 상단, 좌측, 중간의 3부분을 탑재 할 viewportt
//	private HtmlContainer 	northPanel;	// 상단영역
	private ContentPanel	northPanel;
	private ContentPanel 	westPanel;	// 좌측 메뉴영역
	private ContentPanel 	centerPanel;// 중간 프로그램영역
	
	@Override
	protected void initialize() {
		super.initialize();
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		
		createNorth();
		createWest();
		createCenter();
		
		RootPanel.get().add(viewport);
	}
	private void createNorth() {
		HtmlContainer topPanel = null;
		StringBuffer sb = new StringBuffer();
		sb.append("<html>");
		sb.append("<head>");
		sb.append("	<title>Untitled</title>");
		sb.append("	<style>");
		sb.append("	body		{font-family:'';font-size:12px;color:#ffffff;background:#f0f0f0;margin-left:0px;margin-top:0px;}");
		sb.append("	td			{font-family:'';font-size:12px;color:#ffffff;}                                                  ");
		sb.append("	a:link		{text-decoration: none ; color: #ffffff;}                                                         ");
		sb.append("	a:visited	{text-decoration: none ; color: #ffffff;}                                                         ");
		sb.append("	a:active	{text-decoration: none ; color: #ffffff;}                                                         ");
		sb.append("	a:hover		{text-decoration: none ; color: #ffffff;}                                                         ");
		sb.append("	.top		{background-image:url(images/top_bg.gif) ;background-repeat: no-repeat;}                          ");
		sb.append("	</style>                                                                                                      ");
		sb.append("</head>                                                                                                        ");
		sb.append("<body>                                                                                                         ");
		sb.append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">																		");
		sb.append("	<tr>                                                                                                                                        ");
		sb.append("		<td style=\"background-image:url(images/top_bg.gif) ;background-repeat: repeat-x;\">                                                    ");
		sb.append("			<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">                                                             ");
		sb.append("				<tr>                                                                                                                            ");
		sb.append("					<td style=\"font-family:'';font-size:12px;color:#ffffff;\" rowspan=\"2\"><img src=\"images/logo1.gif\" alt=\"oa자산데시보드로고\"></td>                                            ");
		sb.append("					<td style=\"font-family:'';font-size:12px;color:#ffffff;\" align=\"right\" valign=\"top\" style=\"padding-top:15px\">                                                              ");
		sb.append("						<!--a href=\"#\"><img src=\"images/btn_file.gif\" alt=\"file station\" border=\"0\"></a>                                   ");
		sb.append("						<img src=\"images/btn_line.gif\" alt=\"\" style=\"margin-right:10px;margin-left:10px\">                                 ");
		sb.append("						<a href=\"#\"><img src=\"images/btn_audio.gif\" alt=\"audio station\" border=\"0\"></a>                                 ");
		sb.append("						<img src=\"images/btn_line.gif\" alt=\"\" style=\"margin-right:10px;margin-left:10px\">                                 ");
		sb.append("						<a href=\"#\"><img src=\"images/btn_download.gif\" alt=\"download station\" border=\"0\"></a--></td>                      ");
		sb.append("				</tr>                                                                                                                           ");
		sb.append("				<tr>                                                                                                                            ");
		sb.append("					<td style=\"font-family:'';font-size:12px;color:#ffffff;\" align=\"right\" valign=\"bottom\" style=\"padding-bottom:7px\">환영합니다 님 </td>         ");
		sb.append("				</tr>                                                                                                                           ");
		sb.append("			</table>                                                                                                                            ");
		sb.append("		</td>                                                                                                                                   ");
		sb.append("		<td style=\"background-image:url(images/top_bg.gif) ;background-repeat: repeat-x;\" align=\"right\"><img src=\"images/top_right.gif\" alt=\"\"></td>                                                      ");
		sb.append("	</tr>                                                                                                                                       ");
		sb.append("</table>                                                                                                                                     ");
		sb.append("</body>           ");
		sb.append("</html>           ");
		topPanel = new HtmlContainer(sb.toString());
		
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 45);
		data.setMargins(new Margins(0, 5, 5, 5));
		northPanel = new ContentPanel();
		northPanel.setHeaderVisible(false);
		ToolBar tb = new ToolBar();
		Button topBtn = new Button("로그아웃");
		topBtn.setIcon(RiaApp.ICONS.go_out());
		tb.add(topBtn);
		topBtn = new Button("사용자정보수정");
		topBtn.setIcon(RiaApp.ICONS.accordion());
		tb.add(topBtn);
		topBtn = new Button("접속로그");
		topBtn.setIcon(RiaApp.ICONS.text());
		tb.add(topBtn);
		tb.setAlignment(HorizontalAlignment.RIGHT);
		northPanel.setBottomComponent(tb);
		Menu menu = new Menu();   
		  
	    MenuItem item1 = new MenuItem("Edit");   
	    menu.add(item1);   
	  
	    MenuItem item2 = new MenuItem("Open File");   
	    item2.addSelectionListener(buttonListener);
	    menu.add(item2);   
	  
	    Menu sub = new Menu();   
	    sub.add(new MenuItem("readme.txt"));   
	    sub.add(new MenuItem("helloworld.txt"));   
	    item2.setSubMenu(sub);   
	    	
	    MenuBar bar = new MenuBar();   
	    bar.setBorders(true);   
	    bar.setStyleAttribute("borderTop", "none");   
	    bar.add(new MenuBarItem("New", menu));   
	  
	    Menu sub2 = new Menu();   
	    sub2.add(new MenuItem("Cut"));   
	    sub2.add(new MenuItem("Copy"));   
	  
	    MenuBarItem edit = new MenuBarItem("Edit", sub2);   
	    bar.add(edit);   
	  
	    sub = new Menu();   
	    sub.add(new MenuItem("Search"));   
	    sub.add(new MenuItem("File"));   
	    sub.add(new MenuItem("Java"));   
	  
	    MenuBarItem item3 = new MenuBarItem("Search", sub);   
	    bar.add(item3);   
	       
	    menu = new Menu();   
	  
	    CheckMenuItem menuItem = new CheckMenuItem("I Like Cats");   
	    menuItem.setChecked(true);   
	    menu.add(menuItem);   
	  
	    menuItem = new CheckMenuItem("I Like Dogs");   
	    menu.add(menuItem);   
	  
	  
	    menu.add(new SeparatorMenuItem());   
	  
	    MenuItem radios = new MenuItem("Radio Options");   
	    menu.add(radios);   
	  
	    Menu radioMenu = new Menu();   
	    CheckMenuItem r = new CheckMenuItem("Blue Theme");   
	    r.setGroup("radios");   
	    r.setChecked(true);   
	    radioMenu.add(r);   
	    r = new CheckMenuItem("Gray Theme");   
	    r.setGroup("radios");   
	    radioMenu.add(r);   
	    radios.setSubMenu(radioMenu);   
	  
	    MenuItem date = new MenuItem("Choose a Date");   
	    menu.add(date);   
	  
	    date.setSubMenu(new DateMenu());   
	       
	    MenuBarItem item4 = new MenuBarItem("Foo", menu);   
	    bar.add(item4);   
	    northPanel.add(bar);
	    
	    northPanel.setAutoHeight(true);
		viewport.add(northPanel, data);
	}
	/**
	 * 좌측 메뉴영역
	 */
	private void createWest() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 220, 150, 320);
		data.setMargins(new Margins(5, 5, 5, 5));
		data.setCollapsible(true);
		westPanel = new ContentPanel();

		ToolBar toolBar = new ToolBar();
		westPanel.setTopComponent(toolBar);

		viewport.add(westPanel, data);
		Registry.register(WEST_PANEL, westPanel);  // Rigistry에 등록
	}
	/**
	 * 중간 프로그램 영역
	 */
	private void createCenter() {
		centerPanel = new ContentPanel();
		centerPanel.setBorders(false);
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new FitLayout());

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(5, 5, 5, 0));
		viewport.add(centerPanel, data);
		Registry.register(CENTER_PANEL, centerPanel);  // Rigistry에 등록.
	}
	
	public AppView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		
	}
}
